package com.fengjing.launcher.parse;

import android.util.Log;

import com.fengjing.launcher.model.FilmModel;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 * Created by admin on 2017/9/21.
 */

public class FilmParser {

    public static void getFilmList(String rootPath, List<FilmModel> filmModels) {
        File file = new File(rootPath);
        boolean isVR = false, is3D = false, isEpisode = false;

        String name = file.getName();

        if (name.matches(".*3D.*")) {
            is3D = true;
        }

        if (name.matches("(.*jj.*)||(.*zy.*)")) {
            isEpisode = true;
        }

        if (name.matches(".*[V|v][R|r].*")) {
            isVR = true;
        }
        if (name.matches("(.*sm.*)")) {
            isVR = true;
        }
        getFilmList(rootPath, filmModels, isVR, is3D, isEpisode);
    }

    public static void getFilmList(String rootPath, List<FilmModel> filmModels, boolean isVR, boolean is3D, boolean isEpisode) {
        File file = new File(rootPath);

        File[] files = file.listFiles();

        for (int i = 0; i < files.length; i++) {
            FilmModel filmModel = new FilmModel();
            filmModel.setVR(isVR);
            filmModel.set3D(is3D);
            filmModel.setEpisode(isEpisode);

            File file1 = files[i];

            filmModel.setFilmName(file1.getName());
            for (int j = 0; j < file1.listFiles().length; j++) {
                File file3 = file1.listFiles()[j];

                if (filmModel.getFilmPath() == null) {

                    MediaFile.MediaFileType mediaFileType = MediaFile.getFileType(file3.getPath());

                    if (mediaFileType != null && MediaFile.isVideoFileType(mediaFileType.fileType)) {

                        if (isEpisode) {
                            if (file3.getPath().contains("001")) {
                                filmModel.setFilmPath(file3.getPath());
                            }
                        } else {
                            filmModel.setFilmPath(file3.getPath());
                        }
                    }
                }

                if (file3.getPath().endsWith(".png") || file3.getPath().endsWith(".jpg") || file3.getPath().endsWith(".PNG") || file3.getPath().endsWith(".JPG")) {

                    filmModel.setFilmPNG(file3.getPath());
                }

                if (file3.getName().equals("description.txt")){
                    filmModel.setFilmName(AdParser.getDescriptionAttr(file3.getPath(), AdParser.NAME));
                }
            }

            if (filmModel.getFilmPath() == null){
                continue;
            }

            filmModels.add(filmModel);
        }
    }

}
